<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.2
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors: sakuya
 * @LastEditTime: 2021年7月10日20:56:47
-->

<template>
	<el-drawer :title="titleMap[mode]" v-model="visible" :size="700" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left" class="save_form">
			<el-form-item label="任务名称" prop="jobname">
				<el-input v-model="form.jobname" placeholder="任务名称" clearable></el-input>
			</el-form-item>

			<el-form-item label="任务类型" prop="type">
				<el-radio v-model="form.type"  v-for=" item in taskEnumList" :key="item.name" :label="item.name">{{ item.type }}</el-radio>
			</el-form-item>

			<el-form-item  label="执行执行类" prop="clazz">
				<el-input v-model="form.clazz" placeholder="任务执行类" clearable></el-input>
			</el-form-item>

			<el-form-item  label="分组名称" prop="groupName">
				<el-input v-model="form.groupName" placeholder="分组名称" clearable></el-input>
			</el-form-item>

			<el-form-item v-if="form.type=='method'" label="反射类" prop="methodclazz">
				<el-input v-model="form.methodclazz" label="methodclazz">反射类</el-input>
			</el-form-item>

		
			<el-form-item v-if="form.type=='method'" label="反射方法" prop="methodname">
				<el-input v-model="form.methodname" label="methodname">反射执行方法</el-input>
			</el-form-item>

			<el-form-item v-if="form.type=='annotation'" label="注解name" prop="annotationName">
				<el-input v-model="form.annotationName" label="annotationName">注解name</el-input>
			</el-form-item>

			<el-form-item v-if="form.type=='app'" label="应用名称" prop="appName">
				<el-select v-model="form.appName" label="应用名称">
					<el-option
						v-for="item in appoption"
						:key="item.key"
						:label="item.label"
						:value="item.key">
					</el-option>
				</el-select>
			</el-form-item>


			
			<el-form-item v-if="form.type=='app'" label="执行class" prop="appName">
				<el-select v-model="form.appClass" label="执行class">
					<el-option
						v-for="item in appclass"
						:key="item.key"
						:label="item.label"
						:value="item.key">
					</el-option>
				</el-select>
			</el-form-item>

	

			<el-form-item label="定时规则" prop="cron">
				<sc-cron v-model="form.cron" placeholder="请输入Cron定时规则" clearable :shortcuts="shortcuts"></sc-cron>
			</el-form-item>
			<!-- <el-form-item label="是否启用" prop="state">
				<el-switch v-model="form.state" active-value="1" inactive-value="-1"></el-switch>
			</el-form-item> -->

			<el-form-item  label="执行参数" prop="params">
				<el-input v-model="form.params" type="params" :rows="3" label="params">执行参数</el-input>
			</el-form-item>

			<el-form-item  label="任务备注" prop="note">
				<el-input v-model="form.methodname" type="textarea" :rows="3" label="note">任务备注</el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-drawer>
</template>

<script>
	import scCron from '@/components/scCron';
	
	export default {
		components: {
			scCron
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增计划任务',
					edit: '编辑计划任务'
				},
				appoption:[],
				appclass:[],
				form: {
					id:null,
					jobname: "",
					handler: "",
					cron: "",
					state: "1",
					type:"",
					methodclazz:"",
					clazz:"",
					groupName:"",
					methodname:"",
					annotationName:"",
					appName:"",
					appClass:""
				},
				taskEnumList:[],
				rules: {
					methodclazz:[
						{required: true, message: '请填写反射类'}
					],
					methodname:[
						{required: true, message: '请填写任务方法'}
					],
					jobname:[
						{required: true, message: '请填写任务名称'}
					],
					handler:[
						{required: true, message: '请填写执行类'}
					],
					cron:[
						{required: true, message: '请填写定时规则'}
					],
					type:[
						{required: true, message: '请选择任务类型'}
					],
					annotationName:[
						{required: true, message: '请输入注解name'}
					],
					groupName:[
						{required: true, message: '请输入注解分组名称'}
					],
					appName:[
						{required: true, message: '请输入appName名称'}
					]
				},
				visible: false,
				isSaveing: false,
				shortcuts: [
					{
						text: "每天8点和12点 (自定义追加)",
						value: "0 0 8,12 * * ?"
					}
				]
			}
		},
		mounted() {
			this.getEnumList();
			this.getAppOption();
			//监听appName的变化
			this.$watch('form.appName', (newValue, oldValue) => {
				this.getAppJobClass(newValue);
			})
		},
		created(){
			this.$watch('form.type', (newValue, oldValue) => {
				this.watchQuartType(newValue);
    		}, { deep: true });
		},
		methods: {
			async watchQuartType(newValue){
				if(this.taskEnumList.length==0){
					let res=await this.$API.task.getJobEnumList.get();
					this.taskEnumList=res.data;
				}
				let data=this.taskEnumList.find(item => item.name == newValue);
				this.form.clazz=data.quartClazz;
			},
			async getEnumList(){
				let res=await this.$API.task.getJobEnumList.get();
				this.taskEnumList=res.data;
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				
				this.visible = true;
				return this;
			},
			//表单提交方法
			async submit(){
				this.$refs.dialogForm.validate((valid) => {
					if (valid) {
						this.isSaveing = true;
						let res=this.$API.task.addOrUpdateTask.post(this.form);
						if(res.code==200){
							this.$message.success(res.message);
						}
						setTimeout(()=>{
							this.isSaveing = false;
							this.visible = false;
							this.$message.success("操作成功")
							this.$emit('success', this.form, this.mode)
						},1000)
					}
				})
			},
			//获取app下拉选项
			async getAppOption(){
				let res=await this.$API.app.optionlist.get();
				this.appoption=res.data
			},
			//获取appJob的class列表
			async getAppJobClass(appName){
				let res=await this.$API.app.getjobclass.get({'appName':appName});
				this.appclass=res.data
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.jobname = data.jobname
				this.form.handler = data.handler
				this.form.cron = data.cron
				this.form.state = data.state
				this.form.type = data.type
				this.form.methodclazz=data.methodclazz
				this.form.groupName=data.groupName
				this.form.methodname=data.methodname
				this.form.annotationName=data.annotationName
			}
		}
	}
</script>

<style scoped>
	.save_form{
		margin: 0px 15px;
	}
</style>
